
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.util {
/**
 * Comments from wave-protocol
 * Utility functions for checking preconditions and throwing appropriate
 * Errors.
 */
public final class Preconditions {
	function Preconditions() {}
	
	public static function checkElementIndex(index:int, size:int):void {
		org.waveprotocol.wave.model.util.Assert.assert(size >= 0);
		if (index < 0) {
			throw new Error("IndexOutOfBounds: index (" + index + ") must not be negative");
		}
		if (index >= size) {
			throw new Error("IndexOutOfBounds: index (" + index + ") must be less than size (" + size + ")");
		}
	}
	
	public static function checkPositionIndex(index:int, size:int):void {
		Assert.assert(size >= 0);
		if (index < 0) {
			throw new Error("IndexOutOfBounds: index (" + index + ") must not be negative");
		}
		if (index > size) {
			throw new Error("IndexOutOfBounds: index (" + index + ") must not be greater than size (" + size + ")");
		}
	}
	
	public static function checkPositionIndexesInRange(rangeStart:int, start:int, end:int, rangeEnd:int):void {
		Assert.assert(rangeStart >= 0);
		Assert.assert(rangeEnd >= rangeStart);
		
		if (start < rangeStart) {
			throw new Error("IndexOutOfBounds: start index (" + start + ") " +
				"must not be less than start of range (" + rangeStart + ")");
		}
		if (end < start) {
			throw new Error("IndexOutOfBounds: end index (" + end + ") must not be less than start index (" + start + ")");
		}
		if (end > rangeEnd) {
			throw new Error("IndexOutOfBounds: end index (" + end + ") must not be greater than end of range (" + rangeEnd + ")");
		}
	}
	
	public static function checkPositionIndexes(start:int, end:int, size:int):void {
		checkPositionIndexesInRange(0, start, end, size);
	}
	
	public static function checkNotNull(x:Object, errorMessage:Object):Object {
		if (x == null) {
			throw new Error("NullPointer " + errorMessage);
		}
		return x;
	}
	
	/**
	 * Note an illegal argument
	 */
	public static function illegalArgument(errorMessage:Object):void {
		throw new Error("" + errorMessage);
	}
	
	public static function checkArgument(condition:Boolean, errorMessage:Object):void {
		if (!condition) {
			throw new Error("" + errorMessage);
		}
	}
	
	/**
	 * Ensures the truth of an expression involving the state of the calling
	 * instance, but not involving any parameters to the calling method.
	 */
	public static function checkState(expression:Boolean, errorMessage:Object):void {
		if (!expression) {
			throw new Error("Illegal State" + errorMessage);
		}
	}
}
}